package com.itboye.ihomebank.activity.key;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.BangDingBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.SIDBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.interfaces.IRefreshAfterAlert;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.SaveKeyBean;

import java.util.Observable;
import java.util.Observer;

/**
 * 上传锁数据   绑定管理员
 */
public class UpdateKeyActivity extends BaseOtherActivity implements Observer {
    View v_statusbar;
    ImageView close_icon;// 返回图片
    TextView add_shap_title_tv;// 标题
    TextView update_ok;
    KeyPresenter keyPresenter;
    String uid;
    String lockMac,lockName,unlockKey,aesKeystr,lockVersion,adminPss,adminKeyboardPwd,deletePwds,pwdInfo,timestamp,feature;
    int battery;
    @Override
    protected int layout_id() {
        return R.layout.activity_update_key;
    }

    @Override
    protected void onInitController() {

    }
    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        close_icon.setVisibility(View.GONE);
        add_shap_title_tv.setText("上传数据");
        keyPresenter = new KeyPresenter(this);
        uid = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.USER_ID, "");

        lockMac = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.LOCKMAC, "");
        lockName = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.LOCKNAME, "");
        unlockKey = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.UNLOCKKEY, "");
        aesKeystr = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.AESKEYSTER, "");
        lockVersion = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.LOCKVERSION, "");
        adminPss = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.ADMINPSS, "");
        adminKeyboardPwd = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.ADMINKEYBOARPWD, "");
        deletePwds = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.DELETEPWDS, "");
        pwdInfo = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.PWDINGO, "");
        timestamp = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.TIMESTAMP, "");
        feature = (String) SPUtils.get(MyApplcation.ctx, null, SPContants.FEATURE, "");
        battery= (int) SPUtils.get(MyApplcation.ctx,null,SPContants.BATTERY,0);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.update_ok:
                showProgressDialog("正在上传,请稍后......", true);
                String sid=SPUtils.get(this,null,SPContants.LOCK_SESSION,"")+"";
                if ("".equals(sid)){
                    keyPresenter.getAddMain(uid, "6323", lockName,lockName, lockMac, unlockKey, "0",aesKeystr,
                            lockVersion, adminPss, adminKeyboardPwd, deletePwds,pwdInfo, timestamp, feature,battery);
                }else {
                    keyPresenter.getAddMain(sid, "6323", lockName,lockName, lockMac, unlockKey, "0",aesKeystr,
                            lockVersion, adminPss, adminKeyboardPwd, deletePwds,pwdInfo, timestamp, feature,battery);
                }

                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getEventType() == KeyPresenter.addMain_success) {
                ByAlert.alert("添加管理员成功");
                SaveKeyBean.saveData("2",null);
                BangDingBean  bean = (BangDingBean) entity.getData();
                finish();
                Intent intent2 = new Intent("changeavart");
                sendBroadcast(intent2);
            } else if (entity.getEventType() == KeyPresenter.addMain_fail) {
                if (entity.getData() instanceof ServerError ||entity.getData() instanceof TimeoutError){//服务器失败
                    //切换 锁备用服务器
                    showAlert();
                }else {
                    ByAlert.alert("添加管理员失败");
                }

            } else if (entity.getEventType()== UserPresenter.By_GETSID_success){//获取sid 成功
                SIDBean data1 = (SIDBean) entity.getData();
                SPUtils.put(MyApplcation.ctx,null, SPContants.LOCK_SESSION,data1.getSid());
                SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,true);
                toRefreshData();
            }else if (entity.getEventType()==UserPresenter.By_GETSID_fail){
                ByAlert.alert(entity.getMsg());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }


    private void toRefreshData() {
        String sid=SPUtils.get(this,null,SPContants.LOCK_SESSION,"")+"";
        if ("".equals(sid)){
            keyPresenter.getAddMain(uid, "6323", lockName,lockName, lockMac, unlockKey, "0",aesKeystr,
                    lockVersion, adminPss, adminKeyboardPwd, deletePwds,pwdInfo, timestamp, feature,battery);
        }else {
            keyPresenter.getAddMain(sid, "6323", lockName,lockName, lockMac, unlockKey, "0",aesKeystr,
                    lockVersion, adminPss, adminKeyboardPwd, deletePwds,pwdInfo, timestamp, feature,battery);
        }

    }
}
